package artid.com.arttogoa.keeponrecord;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import artid.com.arttogoa.BaseActivity;
import artid.com.arttogoa.FbDynamicActivity;
import artid.com.arttogoa.R;
import artid.com.arttogoa.adapter.ArtImageFragmentAdapter;
import artid.com.arttogoa.app.ArtWorkApp;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.db.DataKeeper;
import artid.com.arttogoa.glide.GlideUtil;
import artid.com.arttogoa.parser.RecordDetailList;
import artid.com.arttogoa.photo.ImagePagerActivity;
import artid.com.arttogoa.task.CallbackSky;
import artid.com.arttogoa.view.SubViewPager;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;

/**
 * 
 * TODO<备案详情>
 * 
 * @author 熊彬
 * @data: 2016-7-4 下午3:21:49
 * @version: V1.0
 */
public class ArtRecordDetailActivity extends BaseActivity implements
		OnClickListener, OnTouchListener {
	String id, state, tittle, imgurl;
	private ImageView reDetailImg, reComplete, Inscribe;
	private TextView reStatus, type, name, size, year, detail, beiantime,
			bianhao, bzdate, edit, infoo;
	private LinearLayout tongguo, weitongguo;
	String realtitle, realdetail, fbtype;
	/**
	 * 
	 */
	private SubViewPager mViewPager;
	private ArrayList<View> pageview;
	// 主适配器
	private ArtImageFragmentAdapter mFragmentAdapter;
	private RelativeLayout r1, r2, r3;
	private ImageView iv1, iv2, iv3;
	private List<ImageView> images;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_art_record_detail);
		ArtWorkApp.getInstance().addArtActivity(ArtRecordDetailActivity.this);
		initview();
	}

	String Is_phone;

	private void initview() {
		id = getIntent().getExtras().getString("id");
		state = getIntent().getExtras().getString("state");
		tittle = getIntent().getExtras().getString("tittle");
		imgurl = getIntent().getExtras().getString("imgurl");
		Is_phone = getIntent().getExtras().getString("Is_phone");
		mContext = this;

		mTittle = (TextView) findViewById(R.id.title_text);
		mFbText = (TextView) findViewById(R.id.title_submit);
		edit = (TextView) findViewById(R.id.redetail_edit);

		r1 = (RelativeLayout) findViewById(R.id.ll_carousel1);
		r2 = (RelativeLayout) findViewById(R.id.ll_carousel2);
		r3 = (RelativeLayout) findViewById(R.id.ll_carousel3);
		images = new ArrayList<ImageView>();
		iv1 = (ImageView) findViewById(R.id.iv_carousel1);
		iv2 = (ImageView) findViewById(R.id.iv_carousel2);
		iv3 = (ImageView) findViewById(R.id.iv_carousel3);
		images.add(iv1);
		images.add(iv2);
		images.add(iv3);
		// mFbText.setTextSize(24);
		mBack = (ImageView) findViewById(R.id.back_btn);

		Inscribe = (ImageView) findViewById(R.id.iv_Inscribe_image);// 落款
		reDetailImg = (ImageView) findViewById(R.id.redeail_img);//
		reComplete = (ImageView) findViewById(R.id.redetail_complete);
		reStatus = (TextView) findViewById(R.id.redetail_status);

		tongguo = (LinearLayout) findViewById(R.id.beianxinxi);
		weitongguo = (LinearLayout) findViewById(R.id.weitongguo);
		infoo = (TextView) findViewById(R.id.tv_info);// 类别
		
		
		type = (TextView) findViewById(R.id.redetail_type);// 类别
		name = (TextView) findViewById(R.id.redetail_nub);// 作者
		size = (TextView) findViewById(R.id.redetail_size);// 尺寸
		year = (TextView) findViewById(R.id.redetail_year);// 年代
		detail = (TextView) findViewById(R.id.redetail_detail);// 详情
		beiantime = (TextView) findViewById(R.id.beiantime);// 备案信息
		bianhao = (TextView) findViewById(R.id.beiancode);// 编号
		bzdate = (TextView) findViewById(R.id.beiandate);// 签发日期
		
		mTittle.setText(tittle);
		mBack.setOnClickListener(this);
		edit.setOnClickListener(this);
 
		mFbText.setOnClickListener(this);
		if (state.equals("-1")) {
			reComplete.setVisibility(View.GONE);
			reStatus.setVisibility(View.VISIBLE);
			reStatus.setText("未通过审核"); 
			weitongguo.setVisibility(View.VISIBLE);
		} else if (state.equals("2")) {
			reComplete.setVisibility(View.VISIBLE);
			reStatus.setVisibility(View.GONE);
			tongguo.setVisibility(View.VISIBLE);
		} else if (state.equals("1")) {
			reComplete.setVisibility(View.GONE);
			reStatus.setVisibility(View.VISIBLE);
			reStatus.setText("审核中");
		}
		reqAtristListData();

	}

	/**
	 * 
	 */
	@SuppressWarnings("deprecation")
	private void initViewPager(ArrayList<RecordDetailList.Pointinformation> list) {
		// TODO Auto-generated method stub
		mViewPager = (SubViewPager) findViewById(R.id.viewPager);
		mFragmentAdapter = new ArtImageFragmentAdapter(
				getSupportFragmentManager(), new CallbackSky() {

					@Override
					public void callBack(int index) {
						imageCom();
					}
				});
		// mViewPager.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// imageCom();
		// }
		// });
		mFragmentAdapter.setData(list);
		mFragmentAdapter.notifyDataSetChanged();
		mViewPager.setAdapter(mFragmentAdapter);
//		mViewPager.setOnTouchListener(this);
		mViewPager.setOffscreenPageLimit(list.size());
		mViewPager.setOnPageChangeListener(mOnPageChangeListener);
		mViewPager.setCurrentItem(0);
		changeImage(0);

	}

	private int lastX = 0;
	private boolean start = false;

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			lastX = (int) event.getRawX();
			System.out.println("currentIndex="+currentIndex+",RawX="+(int) event.getRawX()+",x="+(int) event.getX());
			break;
		case MotionEvent.ACTION_MOVE:
			if ((lastX - event.getRawX()) > 100
					&& (currentIndex == list1.size() - 1)) {
				if (start) {
					imageBrower();
					start = false;
				}
				// Toast.makeText(this, "在此处添加界面跳转代码哦！",
				// Toast.LENGTH_LONG).show();
				// this.finish();
			}
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		if (currentIndex == list1.size() - 1)
			start = true;
		super.onRestart();
		// start = true;
	}

	private int currentIndex;
	private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {
		@Override
		public void onPageSelected(int pos) {
			currentIndex = pos;
			
			if (currentIndex == list1.size() - 1)
				start = true;
			else
				start = false;
			changeImage(pos);
			System.out.println("Selected=" + pos);

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			System.out.println("Scrolled=" + arg0);
//			if (start) {
//				start = false;
//				imageBrower();
//			}
		}

		@Override
		public void onPageScrollStateChanged(int pos) {
			System.out.println("StateChanged=" + pos);
		}
	};

	protected void changeImage(int index) {
		// TODO Auto-generated method stub
		for (int i = 0; i < images.size(); i++) {
			if (i == index) {
				images.get(i).setImageResource(R.drawable.point_pressed);
			} else {
				images.get(i).setImageResource(R.drawable.point_unpressed);
			}
		}
	}

	/**
	 * 打开微观图查看图片
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower() {

		Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, item);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_TYPE,
				ImagePagerActivity.MICSHOW);
		if (Is_phone.equals("2")) {
			intent.putExtra("intentType", 2);//手机备案
		} else if (Is_phone.equals("1")) {
			intent.putExtra("intentType", 1);//设备备案
		}
		mContext.startActivity(intent);
	}

	/**
	 * 打开完整图查看图片
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageCom() {
		ArrayList<String> urls = new ArrayList<String>();
		for (int i = 0; i < list1.size(); i++) {
			urls.add(list1.get(i).getImgurl());
		}
		Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, currentIndex);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_TYPE,
				ImagePagerActivity.LATTICE);
		mContext.startActivity(intent);
	}

	private RecordDetailList item;
	ArrayList<RecordDetailList.Pointinformation> list1;

	// ArrayList<Pointinformation> micList;

	private void reqAtristListData() {
		VolleyBasic.getInstance(ArtRecordDetailActivity.this).post(
				Cache.recorddetaillist,
				VolleyMap.getInstance().recorddetaillist(id),
				RecordDetailList.class, new VolleyCallback() {

					@Override
					public void onSuccess(VolleyData vd) {
						item = (RecordDetailList) vd.getData();
						
						Drawable drawable = null;
						if (Is_phone.equals("2")) {
							drawable = mContext.getResources().getDrawable(
									R.drawable.a_ico_iphone);
						} else if (Is_phone.equals("1")) {
							drawable = mContext.getResources().getDrawable(
									R.drawable.a_ico_shebei);
						}
						// / 这一步必须要做,否则不会显示.
						drawable.setBounds(0, 0, drawable.getMinimumWidth(),
								drawable.getMinimumHeight());
						infoo.setCompoundDrawables(drawable, null, null, null);
						infoo.setCompoundDrawablePadding(20);
						infoo.setText(item.getTitle());
						
						DataKeeper.keepSerializable(mContext, item);
						System.out.println(item.toString());
						ArrayList<RecordDetailList.Pointinformation> list = item
								.getPointinformation();
						list1 = new ArrayList<RecordDetailList.Pointinformation>();
						int sum = 0;
						// list1.add(list.get(0));
						// list1.add(list.get(1));
						if (state.equals("2")) {
							sum = 3;
						} else {
							sum = 2;
							r3.setVisibility(View.GONE);
						}
						for (int i = 0; i < sum; i++) {
							RecordDetailList.Pointinformation p = list.get(i);
							p.setIndex(i);
							list1.add(p);
						}
						initViewPager(list1);

						type.setText(item.getArt_type());
						name.setText(item.getRealname());
						size.setText(item.getWidth() + " X " + item.getHeight());
						year.setText(item.getAge());
						detail.setText(item.getDetail());
						beiantime.setText("申请日期：" + item.getDate());
						bianhao.setText("备案编号：" + item.getCode());// 编号
						if (!TextUtils.isEmpty(item.getIssue_date()))
							bzdate.setText("签发日期：" + item.getIssue_date());
						else
							bzdate.setVisibility(View.GONE);
						realdetail = item.getDetail();
						fbtype = item.getType();
						mFbText.setVisibility(View.VISIBLE);
						if (fbtype.equals("1")) {
							mFbText.setText("发布到动态");
						} else {
							mFbText.setText("已发布");
							mFbText.setClickable(false);
						}
						setInscribe(item.getInscribeimgurl());
					}

					@Override
					public void onError(VolleyError volleyError) {

					}
				});

	}

	public void setInscribe(String url) {
		GlideUtil.setImage(ArtRecordDetailActivity.this, url,
				R.drawable.yspba_img_mor, R.drawable.yspba_img_mor, Inscribe);
		// mImageView.setImageBitmap(getLoacalBitmap(url));
	}

	private RecordDetailList itemNew;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;
		case R.id.redetail_edit:
			itemNew = (RecordDetailList) DataKeeper.readSerializable(mContext,
					RecordDetailList.class);
			itemNew.setId(id);
			System.out.println(itemNew.toString());
			String im1 = itemNew.getComImageUrl();
			if (TextUtils.isEmpty(im1)) {
				Toast.makeText(mContext, "图片正在加载，请稍后~", 2).show();
			} else {
				DataKeeper.keepSerializable(mContext, itemNew);
				Intent intent1 = new Intent(ArtRecordDetailActivity.this,
						ArtRecordEdit.class);
				startActivity(intent1);
			}
			break;
		case R.id.title_submit:

			Intent intent = new Intent(ArtRecordDetailActivity.this,
					FbDynamicActivity.class);
			intent.putExtra("id", id);
			intent.putExtra("state", state);

			intent.putExtra("tittle", tittle);
			intent.putExtra("imgurl", imgurl);
			intent.putExtra("realdetail", realdetail);
			startActivity(intent);
			finish();

			break;

		default:
			break;
		}

	}

	String path;
}
